<?php
/**
 * Application controller
 *
 * This file is the base controller of all other controllers
 *
 * PHP version 5
 *
 */
class AppController extends Controller {
	/**
	 * Components
	 *
	 * @var array
	 * @access public
	 */
	public $components = array(
//		'Security',
		'Auth',
		'Session',
		'RequestHandler',
		'Cookie',
		'DebugKit.Toolbar',
	);

	/**
	 * Helpers
	 *
	 * @var array
	 * @access public
	 */
	public $helpers = array(
		'Html',
		'Form',
		'Session',
		'Text',
		'Js',
		'Time',
	);

	/**
	 * Models
	 *
	 * @var array
	 * @access public
	 */
	public $uses = array(
		'User',
		'Setting',
	);

	/**
	 * Pagination
	 */
	public $paginate = array(
			'limit' => PAGINATE_LIMIT,
	);

	/**
	 * Cache pagination results
	 *
	 * @var boolean
	 * @access public
	 */
	public $usePaginationCache = true;


	/**
	 * beforeFilter
	 *
	 * @return void
	 */
	public function beforeFilter() {
		parent::beforeFilter();
//		$this->Security->blackHoleCallback = 'securityError';

		if (isset($this->request->params['admin'])) {
			$this->layout = 'admin';
		}

		//Check for maintenance status, 'Site Offline' screen is rendered if site is offline.
		if (!isset($this->request->params['admin']) &&
		Configure::read('Site.online') == 0) {
			$this->layout = 'maintenance';
			$this->response->statusCode(503);
			$this->set('title_for_layout', __('Site down for maintenance'));
			$this->render('../Elements/blank');
		}

		if (isset($this->request->params['locale'])) {
			Configure::write('Config.language', $this->request->params['locale']);
		}

		
		$this->Auth->authorize = array('Controller');
		$this->Auth->autoRedirect = false;
		$this->Auth->logoutRedirect = array('controller'=> 'users', 'action'=> 'login');
		$this->Auth->authError = __('You are not authorized to access that location.');
		if (isset($this->request->params['admin'])) {
			$this->Auth->userScope = array('User.role'=> ROLE_ADMINISTRATOR, 'User.status'=> STATUS_ACTIVE);
			$this->Auth->loginRedirect = '/admin';
		}else {
			$this->Auth->userScope = array('User.role'=> array(ROLE_EDITOR, ROLE_AUTHOR, ROLE_PUBLISHER), 'User.status'=> STATUS_ACTIVE);
			$this->Auth->loginRedirect = '/';
		}
	}

	public function isAuthorized($user = null) {
		// Only admins can access admin functions
		if (isset($this->request->params['admin'])) {
			$this->layout = 'admin';
			if(intval($user['role']) !== ROLE_ADMINISTRATOR){
				$this->Session->destroy();
				return false;
			}
		}
		return true;
	}


	/**
	 * afterFilter callback
	 * Disable debug mode on JSON pages to prevent the script execution time to be appended to the page
	 *
	 * @see http://croogo.lighthouseapp.com/projects/32818/tickets/216
	 * @return void
	 */
	public function afterFilter() {
		parent::afterFilter();
		if (!empty($this->params['url']['ext']) && $this->params['url']['ext'] === 'json') {
			Configure::write('debug', 0);
		}
	}

	/**
	 * blackHoleCallback for SecurityComponent
	 *
	 * @return void
	 */
	public function securityError($type) {
		switch ($type) {
			case 'auth':
				break;
			case 'csrf':
				break;
			case 'get':
				break;
			case 'post':
				break;
			case 'put':
				break;
			case 'delete':
				break;
			default:
				break;
		}
		$this->set(compact('type'));
		$this->response = $this->render('../Errors/security');
		$this->response->statusCode(400);
		$this->response->send();
		$this->_stop();
		return false;
	}

}
